﻿package com {
	
	
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import fl.containers.UILoader;
	import fl.transitions.Tween;
	import fl.transitions.easing.*;
	import fl.transitions.TweenEvent;
	import caurina.transitions.*;
	import flash.events.*;
	import flash.text.*;
	import flash.filters.*;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import com.Thumbnail;
	import flash.geom.Rectangle;
	import flash.utils.Timer;
	import fl.controls.ProgressBar; 
	import flash.events.ProgressEvent; 
	import flash.display.StageDisplayState;
	import flash.display.DisplayObject;
	import flash.display.Stage;
	
	public class ThumbnailContener extends Sprite {
		
		static var nb_images:Number = 0;
		public var thumb_holder:Sprite;
		
		var thumb_container_width:Number;
		var thumb_container_height:Number;
		
		var gap_x = 5;
		var gap_y = 5;
		//var columns:Number = 3;
		
		var x_counter:Number = 0;
		var y_counter:Number = 0;
		
		var nodes_XML;
		
		var slideTimer:Timer;
		
		var arrayThumb:Array = new Array;
		
		var thumb_container;
		
		static var current_clicked:Number;//Image cliquée
		
		static var src:String;
		
		var locked_thumb;
		
		public function ThumbnailContener(nodes) {
			nodes_XML = nodes;// constructor code
			//this.addEventListener(MouseEvent.MOUSE_OVER,onOver);
			//this.addEventListener(MouseEvent.MOUSE_OUT,onOut);
			
		}
		
		
		
		function createThumbs() {
			var columns = Main.columns;
			
			thumb_container =  new MovieClip();
			addChild(thumb_container);
			//setChildIndex(thumb_container, 1);
			
			thumb_holder = new Sprite();
			
			addChild(thumb_holder);
			
			thumb_container.mask = thumb_holder;
			thumb_container.y = thumb_holder.y;
			thumb_container.x = 5;
			
			var step_y = 0; 
			var step_x = 0; 
			
			
			
			//Pour les miniatures
			for(var i:String in nodes_XML.elements()) {
				var thumb = new Thumbnail(nodes_XML.image[i].attribute("alt"), parseInt(i), 
										  Main.distant_server+Main.thumb_directory+nodes_XML.image[i].attribute("src"), 
										  Main.thumb_width, 
										  Main.thumb_height, 
										  0x000000, 0xFFB200,
										  0xFFFFFF, 2, 0x000000, 
										  true, true, true);
				
				if( nb_images%columns == 0 ) {
					x_counter = 0;
					step_x = 0;
					if( nb_images != 0) {
						step_y = step_y + thumb.height + Main.row_gap;
					}
				} else {
					step_x = Main.column_gap;
				}
				
				thumb.x = (thumb.width+step_x)*x_counter;
		
				thumb.y = step_y;
				
				nb_images++;
				x_counter++;
				
				thumb.id = i;
				thumb.name = "thumb"+i;
				arrayThumb.push(thumb);
				
				thumb.alpha = 0;
				thumb.scaleX = 0;
				thumb.scaleY = 0;

				thumb_container.addChild(thumb);
				//thumb.addEventListener(MouseEvent.CLICK, onClick)
				
				
				
			}
			
			Main.nb_images = nb_images;
			
			slideTimer = new Timer(150, arrayThumb.length);

			//start transition in
			slideTimer.start();
   			slideTimer.addEventListener("timer", fadeNextImageIn);
			
			var rows = Math.ceil(nb_images/columns);
			
			thumb_container_width = columns *(Main.thumb_width) + (Main.column_gap * (columns-1)) ;
			thumb_container_height = rows * (Main.thumb_height) + Main.row_gap * (rows-1);
			
			
			thumb_holder.graphics.beginFill(0x000000);
			thumb_holder.graphics.drawRect(0,0, Main.animation_width, Main.animation_height - 30);
			thumb_holder.graphics.endFill();
			/*
			thumb_container.graphics.beginFill(0xFFFFFF);
			thumb_container.graphics.drawRect(0,0, thumb_container_width, thumb_container_height);
			thumb_container.graphics.endFill();*/
			
			thumb_holder.x = 0;
			
			
			if(thumb_container_width < thumb_holder.width) {
				thumb_container.x = Main.animation_width/2 - thumb_container_width/2;
			}
			/*
			if(thumb_container_height < thumb_holder.height) {
				thumb_container.y = Main.animation_height/2 - thumb_container_height/2;
			} else {
				thumb_container.y = 10;
				thumb_holder.y = 10;
			}*/
			
			//return thumb_container;

		}
		
		
		//transition in function
		function onClick(e):void {
			
			Main.onClick(e);

			

		}
		

		
		
		
		
		//transition in function
		function fadeNextImageIn(e:Event):void{
			e.target.removeEventListener(Event.COMPLETE, fadeNextImageIn);
			Tweener.addTween(arrayThumb[e.target.currentCount - 1],{alpha:.9, scaleX:1, scaleY:1, time:.4,transition:"easeOut"});

			if((e.target.currentCount) == arrayThumb.length ) {
				dispatchEvent(new Event('thumbs_loaded', true));

			}
		
		}
		
		
		function activateScroll() {
			//this.addEventListener(Event.ENTER_FRAME, moveScrollerThumbs);
		}
		
		
		function resetThumbs(id):void{
			for(var i:uint = 0; i < nb_images; i++){
				var thmb:Object = new Object()
				thmb = thumb_container.getChildByName("thumb"+i);

				if(i == id){
					thmb.clicked = false;
				}
			}
		}
		
		function lock_thumb() {
			
		}
		

			/*
		function moveScrollerThumbs(e:Event):void {

			if( mouseY > thumb_holder.y && mouseY < thumb_holder.y + thumb_holder.height) {
				
				var speed:Number;
				trace(thumb_container.y);
				
				if( thumb_container.width > thumb_holder.width ) {
					
					thumb_container.x += Math.cos(((mouseX)/thumb_holder.width)*Math.PI)*15;
				
					if (thumb_container.x>thumb_holder.x) {
						thumb_container.x = thumb_holder.x ;
					}
					
					if (thumb_container.x<(thumb_holder.x-(thumb_container.width-thumb_holder.width))) {
						thumb_container.x = thumb_holder.x-(thumb_container.width-thumb_holder.width);
					}
				
				}
				
				
				if( thumb_container.height > thumb_holder.height ) {
					thumb_container.y += Math.cos(((mouseY)/thumb_holder.height)*Math.PI)*15;
				
					if (thumb_container.y>thumb_holder.y) {
					thumb_container.y = thumb_holder.y ;
					}
					
					if (thumb_container.y <(thumb_holder.y - (thumb_container.height-thumb_holder.height))) {
						thumb_container.y = thumb_holder.y - (thumb_container.height-thumb_holder.height);
					}
				}
			}
		}*/

	}
	
}
